package com.mcxx.common.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.common.exception.BizException;
import com.mcxx.common.service.InspectionService;
import com.mcxx.modules.miniSalvation.order.dto.InspectionBatchDTO;
import com.mcxx.modules.miniSalvation.order.dto.InspectionOrderDTO;
import com.mcxx.modules.miniSalvation.order.entity.InspectionBatchEntity;
import com.mcxx.modules.miniSalvation.order.entity.OrderInspectionEntity;
import com.mcxx.modules.miniSalvation.order.param.InspectionBatchParam;
import com.mcxx.modules.miniSalvation.order.param.InspectionOrderParam;
import com.mcxx.modules.miniSalvation.order.service.read.InspectionBatchReadService;
import com.mcxx.modules.miniSalvation.order.service.read.InspectionOrderReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderInspectionReadService;
import com.mcxx.modules.miniSalvation.order.service.write.InspectionBatchWriteService;
import com.mcxx.modules.miniSalvation.order.service.write.OrderInspectionWriteService;
import com.mcxx.modules.system.dto.CustomUserDTO;
import com.mcxx.modules.system.service.read.CustomUserReadService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.utils.ResponseExportUtil;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;



@Api("数据督查控制类")
@RestController
@RequestMapping("inspection/v2")
public class InspectionController extends DefaultRestService {

    private final static Logger logger = LoggerFactory.getLogger(InspectionController.class);

    @Autowired
    private InspectionService inspectionService;
    @Autowired
    private InspectionBatchReadService inspectionBatchReadService;
    @Autowired
    private InspectionBatchWriteService inspectionBatchWriteService;
    @Autowired
    private InspectionOrderReadService inspectionOrderReadService;
    @Autowired
    private OrderInspectionReadService orderInspectionReadService;
    @Autowired
    private CustomUserReadService customUserReadService;
    @Autowired
    private OrderInspectionWriteService orderInspectionWriteService;

    
    @PostMapping("createBatch")
    @ApiOperation("创建批次")
    public void createBatch(@RequestBody InspectionBatchDTO batchDTO) {
        logger.info("创建批次，参数：{}", JSON.toJSONString(batchDTO));
        inspectionService.createBatch(batchDTO, getCurUserAccount());
        logger.info("创建批次完成");
    }

    
    @GetMapping("listUserByAreaCode")
    @ApiOperation("查询批次列表")
    public void listUserByAreaCode(@V({"notnull"}) String areaCode) {
        logger.info("根据行政区划编码获取用户列表，参数：{}", areaCode);
        List<CustomUserDTO> users = customUserReadService.listByAreaCode(areaCode);
        setData(users);
        logger.info("根据行政区划编码获取用户列表完成，结果：{}", JSON.toJSONString(users));
    }

    
    @PostMapping("listBatch")
    @ApiOperation("查询批次列表")
    public void listBatchByCondition(InspectionBatchParam param, PageParam pageParam) {
        logger.info("查询批次列表，请求参数：{}，分页参数：{}", JSON.toJSONString(param), JSON.toJSONString(pageParam));
        Page page = inspectionBatchReadService.listByCondition(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询批次列表完成，结果：{}", JSON.toJSONString(page));
    }

    
    @PostMapping("listOrderNotInBatch")
    @ApiOperation("查询未进入批次的业务")
    public void listOrderNotInBatch(InspectionOrderParam param, PageParam pageParam) {
        logger.info("查询未进入批次的业务，请求参数：{}，分页参数：{}", JSON.toJSONString(param), JSON.toJSONString(pageParam));
        Page page = inspectionOrderReadService.listNotInBatch(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询未进入批次的业务完成，结果：{}", JSON.toJSONString(page));
    }

    
    @PostMapping("listOrderInBatch")
    @ApiOperation("查询批次中的业务")
    public void listOrderInBatch(InspectionOrderParam param, PageParam pageParam) {
        logger.info("查询批次中的业务，请求参数：{}，分页参数：{}", JSON.toJSONString(param), JSON.toJSONString(pageParam));
        Page page = inspectionOrderReadService.listByCondition(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询批次中的业务完成，结果：{}", JSON.toJSONString(page));
    }

    
    @PostMapping("addBatchItem")
    @ApiOperation("添加业务至批次")
    public void addBatchItem(@V({"notnull"}) String batchId,@V({"notnull"}) String orderIds) {
        logger.info("添加业务{}至批次{}", batchId, orderIds);
        inspectionService.addBatchItem(batchId, Arrays.asList(orderIds.split(",")));
        logger.info("添加业务{}至批次{}完成", batchId, orderIds);
    }

    
    @PostMapping("deleteBatchItem")
    @ApiOperation("删除督查批次中的批次项")
    public void deleteBatchItem(@V({"notnull"}) String batchId,@V({"notnull"}) String items) {
        logger.info("删除批次{}中的{}", batchId, items);
        inspectionService.deleteBatchItem(batchId, Arrays.asList(items.split(",")));
        logger.info("删除批次{}中的{}完成", batchId, items);
    }

    
    @PostMapping("deleteBatch")
    @ApiOperation("删除督查批次")
    public void deleteBatch(String id) {
        logger.info("删除督查批次，id：{}", id);
        inspectionService.deleteBatch(id);
        logger.info("删除督查批次完成");
    }

    
    @PostMapping("updateBatchState")
    @ApiOperation("修改批次状态")
    public void updateBatchState(@V({"notnull"}) String id, @V({"notnull"}) String state) {
        logger.info("修改批次{}的状态为{}", id, state);
        InspectionBatchEntity batchEntity = new InspectionBatchEntity();
        batchEntity.setId(id);
        batchEntity.setBatchState(state);
        batchEntity.setUpdateName(getCurUserAccount().getName());
        batchEntity.setUpdateTime(new Date());
        inspectionBatchWriteService.updateBatchState(batchEntity);
        logger.info("修改批次状态完成");
    }

    
    @PostMapping("pushBatch")
    @ApiOperation("推送督查批次")
    public void pushBatch(String id) {
        logger.info("推送督查批次，参数：{}", id);
        inspectionBatchWriteService.pushBatch(id, getCurUserAccount());
        logger.info("推送督查批次完成");
    }

    
    @PostMapping("saveInspectionDetail")
    @ApiOperation("添加督查记录")
    public void saveInspectionDetail(InspectionBatchParam param) {
        logger.info("添加督查记录，参数：{}", JSON.toJSONString(param));

        logger.info("添加督查记录完成");
    }

    
    @GetMapping("getInspectionDetail")
    @ApiOperation("查询督查记录")
    public void getInspectionDetail(String batchOrderId) {
        logger.info("查询督查记录，参数：batchOrderId：{}", batchOrderId);
        OrderInspectionEntity entity = orderInspectionReadService.getByBatchOrderId(batchOrderId);
        setData(entity);
        logger.info("查询督查记录完成，结果：{}", JSON.toJSONString(entity));
    }

    
    @PostMapping("exportInspectors")
    @ApiOperation("导出督查人员信息")
    public void exportInspector(HttpServletResponse response, @V({"notnull"}) String batchId) {
        logger.info("导出督查人员信息，参数：{}", batchId);
        ExcelInfo excelInfo = inspectionService.exportInspectors(batchId,getCurUserAccount());
        try {
            ResponseExportUtil.exportFileWithBytes(response, excelInfo.getFile(), excelInfo.getFileName());
        } catch (IOException e) {
            throw new BizException(FailureCode.ERR_9999);
        }
        logger.info("导出督查人员信息完成");
    }

    
    @PostMapping("statisticalReport")
    @ApiOperation("查看统计报表")
    public void statisticalReport(InspectionBatchParam param) {
        logger.info("查看统计报表，参数：{}", JSON.toJSONString(param));

        logger.info("查看统计报表完成");
    }


    
    @PostMapping("listInspectionResult")
    @ApiOperation("查询督查结果列表")
    public void listInspectionResult(InspectionOrderParam param, PageParam pageParam) {
        logger.info("查询督查结果列表，请求参数：{}，分页参数：{}", JSON.toJSONString(param), JSON.toJSONString(pageParam));
        Page page = inspectionOrderReadService.listResultByCondition(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询督查结果列表完成，结果：{}", JSON.toJSONString(page));
    }

    
    @PostMapping("listAuditBatch")
    @ApiOperation("查询批次列表")
    public void listAuditBatchByCondition(InspectionBatchParam param, PageParam pageParam) {
        logger.info("查询批次列表，请求参数：{}，分页参数：{}", JSON.toJSONString(param), JSON.toJSONString(pageParam));
        Page page = inspectionBatchReadService.listAuditBatchByCondition(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询批次列表完成，结果：{}", JSON.toJSONString(page));
    }

    
    @PostMapping("saveDataAudit")
    @ApiOperation("数据核实")
    public void saveDataAudit(@RequestBody InspectionOrderDTO batchDTO) {
        logger.info("数据核实，参数：{}", JSON.toJSONString(batchDTO));
        orderInspectionWriteService.saveDataAudit(batchDTO, getCurUserAccount());
        logger.info("数据核实完成");
    }
}
